package com.alipay.sdk.util;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @ProjectName: 团餐
 * @ClassName: DateUtil
 * @Description: 日期工具类
 * @Author: bd-001935
 * @CreateDate: 4/17/22 8:42 PM
 * @UpdateUser: bd-001935
 * @UpdateDate: 4/17/22 8:42 PM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class DateUtil {

    public static String getSystemTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date(System.currentTimeMillis());//获取系统时间
        return simpleDateFormat.format(date);
    }

    public static String getCurrentDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setTimeZone(TimeZone.getTimeZone("GMT+08")); //设置时区
        Date curDate = new Date(System.currentTimeMillis()); //获取当前时间
        String dateStr = format.format(curDate);   //格式转换
        return dateStr;
    }

    public static String getCurrentDateNoLine() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        format.setTimeZone(TimeZone.getTimeZone("GMT+08")); //设置时区
        Date curDate = new Date(System.currentTimeMillis()); //获取当前时间
        String dateStr = format.format(curDate);   //格式转换
        return dateStr;
    }

    public static String getCurrentDateTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        format.setTimeZone(TimeZone.getTimeZone("GMT+08")); //设置时区
        Date curDate = new Date(System.currentTimeMillis()); //获取当前时间
        String dateStr = format.format(curDate);   //格式转换
        return dateStr;
    }

    public static String getCurrentDateTimeNormal() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("GMT+08")); //设置时区
        Date curDate = new Date(System.currentTimeMillis()); //获取当前时间
        String dateStr = format.format(curDate);   //格式转换
        return dateStr;
    }

    public static String formatTime(String original) {
        if (!TextUtils.isEmpty(original)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date date = simpleDateFormat.parse(original);
                SimpleDateFormat newSf = new SimpleDateFormat("MM-dd HH:mm");
                return newSf.format(date);
            } catch (ParseException e) {
                e.printStackTrace();
                return "";
            }
        }
        return "";
    }

    public static String formatTimeNoLine(String original) {
        if (!TextUtils.isEmpty(original)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            try {
                Date date = simpleDateFormat.parse(original);
                SimpleDateFormat newSf = new SimpleDateFormat("MM-dd HH:mm");
                return newSf.format(date);
            } catch (ParseException e) {
                e.printStackTrace();
                return "";
            }
        }
        return "";
    }

    public static long formatTimeMillis(String original) {
        if (!TextUtils.isEmpty(original)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            try {
                Date date = simpleDateFormat.parse(original);
                return date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return System.currentTimeMillis();
    }

    /**
     * 获得某天的过去/未来几天
     *
     * @param oneday
     * @param amount
     * @return
     */
    public static Date daysAgoOrAfter(Date oneday, int amount) {
        Calendar mon = Calendar.getInstance();
        mon.setTime(oneday);
        mon.add(Calendar.DATE, amount);
        return mon.getTime();
    }
}
